package com.arnold.demo.journal.service

import java.text.SimpleDateFormat
import java.util.Date

import com.arnold.demo.journal.common.InvoiceType
import com.arnold.demo.journal.config.Config

import scala.util.control.Breaks
import java.util.Calendar
import java.util.GregorianCalendar

/**
  * Created by arnold.zhu on 2017/7/24.
  */
object ParseService {

  // 获取日期
  def getInvoiceDate(header: Array[String]): String = {
    val uniqueFormat = new SimpleDateFormat("ddMMM yy HH:mm")
    val uniqueFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
    try {
      if (Config.MOCK_DATA) {
        val calendar = new GregorianCalendar
        calendar.setTime(new Date())
//        calendar.add(Calendar.DATE, -20)
        uniqueFormat2.format(calendar.getTime)
      } else {
        val containsDateStr: String = header(header.length - 1)
        val dateStrs: Array[String] = containsDateStr.split(" ")
        var strDate: String = dateStrs(dateStrs.length - 2) + " " + dateStrs(dateStrs.length - 1)
        strDate = strDate.replace("'", " ")

        val udate: Date = uniqueFormat.parse(strDate)
        uniqueFormat2.format(udate)
      }
    } catch {
      case e: Exception => {
        println("error:====" + e.getMessage)
        ""
      }
    }
  }

  // 获取小票类型
  def getTransType(originType: String, subPay: Double, value: Double): Int = {
    var itype = 0
    if ("Wechat".equalsIgnoreCase(originType)) {
      itype = InvoiceType.WECHAT
    } else if ("IP Bank".equalsIgnoreCase(originType)) {
      itype = InvoiceType.BANK
    } else if ("Cash".equalsIgnoreCase(originType)) {
      itype = InvoiceType.CASH
    } else if ("SVC".equalsIgnoreCase(originType) || "SSTRedeem".equalsIgnoreCase(originType)) {
      itype = InvoiceType.SVC
    } else {
      itype = InvoiceType.OTHERS
    }
    if (subPay.compareTo(value) < 0) {
      if (itype == InvoiceType.WECHAT) {
        itype = InvoiceType.MIX_WITH_WECHAT
      } else {
        itype = InvoiceType.MIX_WITHOUT_WECHAT
      }
    }
    itype
  }

  // 获取小票金额
  def getInvoiceValueAndType(body: Array[String]): (Double, String) = {
    var subTotalIndex: Int = 0

    val loop = new Breaks
    loop.breakable {
      for (i <- body.indices) {
        if (body(i).contains("Subtotal")) {
          subTotalIndex = i
          loop.break()
        }
      }
    }

    if (subTotalIndex != 0) {
      // cash,paid
      val typeStr: String = body(subTotalIndex - 1)
      val valueStr = body(subTotalIndex + 1)

      val types: Array[String] = typeStr.trim.split(" ")
      val values: Array[String] = valueStr.trim().split(" ")

      val originType = types(0) + types(1)

      // 小票类型
      val subPayStr: String = types(types.length - 1)
      val subPay = subPayStr.toDouble

      val invoiceValue = values(values.length - 1).toDouble
      val invoiceType = getTransType(originType.trim(), subPay, invoiceValue)

      Tuple2(invoiceValue, invoiceType.toString)
    } else {
      Tuple2(0.00, "1")
    }
  }


}
